package com.example.campuslink.viewModel


import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.ui.activity.UserMainActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ScanViewModel : BaseViewModel() {
    private val _schoolUserNumber = MutableLiveData<SchoolInfoTable?>()
    val schoolUserNumber get() = _schoolUserNumber
    fun jumpToUserMainActivity(number: String) {
        startActivity(Intent().apply {
            putExtra(putExtraKey.USER_NUMBER.key, number)
        }, targetClass = UserMainActivity::class.java, finishCurrent = true)
    }

    fun getNowNumberInfo(telNumber: String) {
        viewModelScope?.launch(Dispatchers.IO) {
            val userInfoTable = userNumberDao.getUserNumber(telNumber)
            if (userInfoTable != null) {
                _schoolUserNumber.postValue(schoolInfoDao.getSchoolInfoBySchoolName(userInfoTable.schoolName))
            }
        }
    }
}